package it.softphone.cmc.server.entity;

import it.softphone.cmc.shared.entity.Attributes;
import it.softphone.commons.json.server.JSONConverter;
import it.softphone.commons.json.server.JSONException;
import it.softphone.commons.json.server.JSONUtils;
import it.softphone.commons.service.LogAware;


/**
 * 
 * @author bsorrentino
 *
 */
public abstract class AbstractJSONAttributesConverter implements JSONConverter<Attributes>, LogAware {

	protected abstract Attributes makeAttributes();
	
	@SuppressWarnings("rawtypes")
	@Override
	public final Object convert(Class type, Object value) {

		Attributes attrs = makeAttributes();
		
		if( value instanceof it.softphone.commons.json.server.JSONObject ) {
			
			try {
				JSONUtils.copyTo(attrs, (it.softphone.commons.json.server.JSONObject)value);
			}
			catch( JSONException e ) {

				logger.warn("error converting JSON object to attributes", e);
			}
		}

		return attrs;
	}

	@Override
	public final Object convert( Attributes value) {
		return new it.softphone.commons.json.server.JSONObject(value);
	}


}
